package com.bdqn.bookshop.config;

import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.interceptor.AdminLoginInterceptor;
import com.bdqn.bookshop.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.awt.print.Book;
import java.io.File;


/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 14:46
 **/
@Configuration
public class MyWebMvcConfiguration implements WebMvcConfigurer  {

    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;

    @Autowired
    private LoginInterceptor loginInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加一个拦截器，拦截以/admin为前缀的url路径（后台登陆拦截）
        registry.addInterceptor(adminLoginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/login.html");
        //前台拦截器
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/*")
                .excludePathPatterns("/mall/join.html#tologin")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/registerUser")
                .excludePathPatterns("/static/*")
                ;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/admin").setViewName("admin/login");
        registry.addViewController("/").setViewName("mall/index");
        registry.addViewController("/index.html").setViewName("mall/index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        File upload = new File("");
        String path = upload.getAbsolutePath();
        path = path.substring(0,path.lastIndexOf(File.separator));
        path+=File.separator+"upload";
       // path = path.replaceAll("\\\\", "/");


        //重置保存地址
        BookConstants .resetNewPath("file:///"+path+File.separator, "public/");
        File   file=new File(BookConstants.SAVE_PATH.substring("file:///".length()));
        if(!file.exists()) {
            file.mkdirs();
        }
        System.out.println(BookConstants.MAPPING_PATH);
        System.out.println(BookConstants.SAVE_PATH);
        System.out.println(path);
        registry.addResourceHandler("/"+BookConstants.MAPPING_PATH+"**")//回显地址
                .addResourceLocations(BookConstants.SAVE_PATH);//上传地址

//        registry.addResourceHandler("/public/**").
//                addResourceLocations("file:///"+path+File.separator);
    }
}
